/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年6月15日
 * @version 0.1
 */
package com.suredy.base.ctrl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.base.model.Menu;
import com.suredy.base.model.Resource;
import com.suredy.base.model.ResourceType;
import com.suredy.base.service.MenuSrv;
import com.suredy.base.service.ResourceSrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.tools.SuredyMessageModel;


/**
 * @author ZhangMaoren
 *
 */
@Controller
@RequestMapping(value="config")
public class ResourceCtrl extends BaseCtrl{
	private final static Logger log = LoggerFactory.getLogger(ResourceCtrl.class);
	private final String MSG_RESOURCE_NOT_FOUND = "未能资源数据，请查看log获取更多错误信息！";
	
	@Autowired
	private MenuSrv menuSrv;
	
	@Autowired
	private ResourceSrv resourceSrv;
	/**
	 * get resouce list
	 * 
	 * @param type
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "resource-list")
	public ModelAndView getResources() {
		ModelAndView view = new ModelAndView("config/resource-list");
		List<Resource> data = this.resourceSrv.getAll();
		view.addObject("data", data);
		return view;
	}
		
	/**
	 * get resource by id.
	 * 
	 * @param resourceId
	 * @return
	 */
	@RequestMapping("resource-edit")
	public ModelAndView edit(String resourceId) {	
		ModelAndView view = new ModelAndView("config/resource-edit");
		if (StringUtils.isBlank(resourceId)) {
			log.info("Parameter String[id] is blank.");
			return view;
		} 
		Resource data = this.resourceSrv.getById(resourceId);
		if (data == null) {
			log.error("Can not get resource data by id[" + resourceId + "].");
			return this.gotoErrorPage(this.MSG_RESOURCE_NOT_FOUND);
		}
		view.addObject("types", ResourceType.values());		
		view.addObject("resource", data);
		return view;
	}
	
	/**
	 * create resource.
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("resource-create")
	public ModelAndView create() {	
		ModelAndView view = new ModelAndView("config/resource-create");
		view.addObject("types", ResourceType.values());		
		return view;
	}
	
	/**
	 * save resource.
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("resource-save")
	@ResponseBody
	public Object save(String id, String name, String uri, String type) {
		if (StringUtils.isBlank(name) || StringUtils.isBlank(uri)) {
			return SuredyMessageModel.createErrorMessage("参数不足, URI和名称必须填写", null);
		}
		
		Date date = new Date();
		if (!StringUtils.isBlank(id)){
			Resource resource = this.resourceSrv.get(id);
			resource.setId(id);
			resource.setName(name);
			resource.setUri(uri);
			resource.setType(Integer.parseInt(type));
			resource.setLastModifiedTime(date);
			
			this.resourceSrv.update(resource);
		} else {
			Resource n = new Resource();
			n.setName(name);
			n.setUri(uri);
			n.setType(Integer.parseInt(type));
			n.setCreateTime(date);
			n.setLastModifiedTime(date);
			n = this.resourceSrv.save(n);
		}
		
		return SuredyMessageModel.createSuccessMessage(null, null);
	}
	
	/**
	 * delete resource.
	 * 
	 * @param resourceId
	 * @return
	 */
	@RequestMapping("resource-remove")
	@ResponseBody
	public Object delete(String resourceId) {
		if (StringUtils.isBlank(resourceId)) {
			return SuredyMessageModel.createErrorMessage("参数不足, 必须提供资源ID", null);
		}
		Resource r = this.resourceSrv.getById(resourceId);
		this.resourceSrv.delete(r);
		List<Menu> menus = this.menuSrv.getByResource(resourceId);
		for (Menu menu : menus) {
			menu.setResourceId(null);
			this.menuSrv.update(menu);
		}
		return SuredyMessageModel.createSuccessMessage(null, null);
	}
}
